//navbar background
$(".navbar-toggle").click(function(){
	$(".navbar-header").toggleClass("active");
	$(".navbar-default .navbar-toggle .icon-bar").toggle();
	$(".navbar_close").toggle();
	$(".navbar-brand").find(".logo_img").toggle();
	$("#shownav").toggleClass("active")
	$('html,body').toggleClass('ovfHiden');
	$('.body_shade').toggle()
})

// 返回顶部
$(document).ready(function($) {
	var offset = 0,
		offset_opacity = 1200,
		scroll_top_duration = 200,
		$back_to_top = $('.cd-top');

	$(window).scroll(function() {
		($(this).scrollTop() > offset) ? $back_to_top.addClass('cd-is-visible'): $back_to_top.removeClass('cd-is-visible cd-fade-out');
		if($(this).scrollTop() > offset_opacity) {
			$back_to_top.addClass('cd-fade-out');
		}
	});
	$back_to_top.on('click', function(event) {
		event.preventDefault();
		$('body,html').animate({
			scrollTop: 0,
		}, scroll_top_duration);
	});
});

function yearNum() {
	$('#yearNum').html(new Date().getFullYear());
}


//load动画
window.onload = function() {
	$(".load").fadeOut(1000);
	yearNum();
}
var wave = (function() {
	var ctx;
	var waveImage;
	var canvasWidth;
	var canvasHeight;
	var needAnimate = false;

	function init(callback) {
		var wave = document.getElementById('wave');
		var canvas = document.createElement('canvas');
		if(!canvas.getContext) return;
		ctx = canvas.getContext('2d');
		canvasWidth = wave.offsetWidth;
		canvasHeight = wave.offsetHeight;
		canvas.setAttribute('width', canvasWidth);
		canvas.setAttribute('height', canvasHeight);
		wave.appendChild(canvas);
		waveImage = new Image();
		waveImage.onload = function() {
			waveImage.onload = null;
			callback();
		}
		waveImage.src = 'img/wave.png';
	}

	function animate() {
		var waveX = 0;
		var waveY = 0;
		var waveX_min = -203;
		var waveY_max = canvasHeight * 1.1;
		var requestAnimationFrame =
			window.requestAnimationFrame ||
			window.mozRequestAnimationFrame ||
			window.webkitRequestAnimationFrame ||
			window.msRequestAnimationFrame ||
			function(callback) {
				window.setTimeout(callback, 100);
			};

		function loop() {
			ctx.clearRect(0, 0, canvasWidth, canvasHeight);
			if(!needAnimate) return;
			if(waveY < waveY_max) waveY += 1.5;
			if(waveX < waveX_min) waveX = 0;
			else waveX -= 3;

			ctx.globalCompositeOperation = 'source-over';
			ctx.beginPath();
			ctx.arc(canvasWidth / 2, canvasHeight / 2, canvasHeight / 2, 0, Math.PI * 2, true);
			ctx.closePath();
			ctx.fill();

			ctx.globalCompositeOperation = 'source-in';
			ctx.drawImage(waveImage, waveX, canvasHeight - waveY);

			requestAnimationFrame(loop);
		}
		loop();
	}

	function start() {
		if(!ctx) return init(start);
		needAnimate = true;
		setTimeout(function() {
			if(needAnimate) animate();
		},100);
	}

	function stop() {
		needAnimate = true;
	}
	return {
		start: start,
		stop: stop
	};
}());
wave.start();

//监听nav
window.onscroll = function() {
	var t = document.documentElement.scrollTop || document.body.scrollTop;
	if(t >= 28) {
		$(".navbar").addClass("active");
	} else {
		$(".navbar").removeClass("active");
	}
};

